package logiexport.wizard;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import logiexport.carrito.PanelCarrito;
import logiexport.carrito.ProductSingleton;
import logiexport.pojos.Producto;
import logiexport.bd.*;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.wizard.Wizard;
import org.apache.wicket.extensions.wizard.WizardModel;
import org.apache.wicket.extensions.wizard.WizardStep;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;
import org.odlabs.wiquery.ui.accordion.Accordion;

public class BuyWizard extends Wizard {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Ojo lo ideal es cargar el array en otra pagina y no en esta para hacer la carga mas rapida
	private List<Integer> cantidad = Arrays.asList(new Integer[] {
			1,2,3 });
	
	private PanelCarrito carrito;
	private App app = new App();
	
	
	

	private final class RosasStep extends WizardStep { 
		
	/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		//	private int selectedFruit;
		private boolean cRS=false;

		public RosasStep() {
		
			Accordion	accordion = new Accordion("accordion");
			add(accordion);
			
			accordion.setCollapsible(false);
			
			System.out.println("???????Active"+accordion.getActive());
			
			
	
		final	List<Producto> listap;		
		
		listap = app.productos();
		
		
		System.out.println("Cargando productos: "+listap.size());
		
		  //------------------ Lista rosas sin montar ------
			
			@SuppressWarnings("unchecked")
			ListView listview = new ListView("RSlist", listap){
				
				private static final long serialVersionUID = 1L;

				@SuppressWarnings("rawtypes")
				@Override
				protected void populateItem(ListItem item) {
					
					// Tomo el usuario correspondiente a este item de la lista
					 final Producto p = (Producto) item.getModelObject();
					 
					 Form<String> addcarform = new Form<String>("productform");	
					 
					 AjaxButton addcarbutton = new AjaxButton("addcar") {

						 private static final long serialVersionUID = 1L;

						@Override
						protected void onSubmit(AjaxRequestTarget target,
								Form<?> form) {
							// TODO Auto-generated method stub
							//super.onSubmit(target, form);					

							ProductSingleton.getInstance().add(p);											
							carrito.configure();							
							target.add(carrito);
							
						}
						 
						 
					};				
					 
			
					 
					 addcarform.add(addcarbutton);
					 addcarform.add(new Label("rsTallo", p.getTallo()));
					 addcarform.add(new ListChoice<Integer>("sRS", new PropertyModel<Integer>(p, "unidades"), cantidad));
					 		
					 
					 item.add(addcarform);
					
					 
					
				}
			};
			
			
			
			// Lista rosas montadas
			
			
			@SuppressWarnings("unchecked")
			ListView rmlist = new ListView("RMlist", listap){
			    
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				protected void populateItem(ListItem item) {
					
					// Tomo el usuario correspondiente a este item de la lista
					 final Producto p = (Producto) item.getModelObject();
					 
					 item.add(new Label("rmNombre", p.getNombre()));					 
					 item.add(new Label("rmTallo", p.getTallo()));

					
				}
			};
			
			accordion.add(rmlist);
			
			
			accordion.add(listview); 		

			
		}

	}
	
	
	
	public BuyWizard(String id, boolean addDefaultCssStyle, PanelCarrito panel) {
		super(id, addDefaultCssStyle);
		// TODO Auto-generated constructor stub
		
		
		carrito = panel;
	
		WizardModel model = new WizardModel();
        model.add(new RosasStep());     
        
        
        
        init(model);
		
	}


	@Override
	protected void onConfigure() {
		// TODO Auto-generated method stub
		super.onConfigure();
		
		System.out.println("Dentro del conf del buy");
	}
	





}
